package software.hust.com.hust_last.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import software.hust.com.hust_last.R;


public class MTitleBar extends CardView {

    private Context mContext;

    private int mMargin;
    private int mIconSize;

    private OnClickListener mRightListener;

    public MTitleBar(Context context) {
        this(context, null);
    }

    public MTitleBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MTitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        mMargin = ScreenUtil.dp2px(context, 15);
        mIconSize = ScreenUtil.dp2px(context, 45);
        setRadius(0);
        setCardElevation(ScreenUtil.dp2px(context, 10));

        RelativeLayout layout = new RelativeLayout(context);
        addView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

        TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MTitleBar, defStyleAttr, 0);
        String title = array.getString(R.styleable.MTitleBar_title);
        String leftText = array.getString(R.styleable.MTitleBar_leftText);
        String rightText = array.getString(R.styleable.MTitleBar_rightText);
        Drawable leftDrawable = array.getDrawable(R.styleable.MTitleBar_leftDrawable);
        Drawable rightDrawable = array.getDrawable(R.styleable.MTitleBar_rightDrawable);

        if (!TextUtils.isEmpty(title)) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            TextView titleTv = new TextView(context);
            titleTv.setText(title);
            titleTv.setTextColor(Color.WHITE);
            titleTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
            layout.addView(titleTv, params);
        }

        if (leftDrawable != null) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mIconSize, mIconSize);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            ImageView leftIv = new ImageView(context);
            leftIv.setImageDrawable(leftDrawable);
            leftIv.setScaleType(ImageView.ScaleType.CENTER);
            if (mContext instanceof Activity) {
                leftIv.setOnClickListener(mBackListener);
            }
            layout.addView(leftIv, params);
        }

        if (!TextUtils.isEmpty(leftText)) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.setMargins(mMargin, 0, 0, 0);
            TextView leftTv = new TextView(context);
            leftTv.setText(leftText);
            leftTv.setTextColor(Color.WHITE);
            leftTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            if (mContext instanceof Activity) {
                leftTv.setOnClickListener(mBackListener);
            }
            layout.addView(leftTv, params);
        }

        if (rightDrawable != null) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mIconSize, mIconSize);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            ImageView rightIv = new ImageView(context);
            rightIv.setImageDrawable(rightDrawable);
            rightIv.setScaleType(ImageView.ScaleType.CENTER);
            rightIv.setOnClickListener(mInnerRightListener);
            layout.addView(rightIv, params);
        }

        if (!TextUtils.isEmpty(rightText)) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.setMargins(0, 0, mMargin, 0);
            TextView rightTv = new TextView(context);
            rightTv.setText(rightText);
            rightTv.setTextColor(Color.WHITE);
            rightTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            rightTv.setOnClickListener(mInnerRightListener);
            layout.addView(rightTv, params);
        }
    }

    public void setRightButtonListener(OnClickListener listener) {
        this.mRightListener = listener;
    }

    private OnClickListener mInnerRightListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mRightListener != null) {
                mRightListener.onClick(v);
            }
        }
    };

    private OnClickListener mBackListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mContext instanceof Activity) {
                ((Activity) mContext).finish();
            }
        }
    };
}
